package com.example.taobaounion.ui.fragment;

import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseFragment;
import com.example.taobaounion.model.bean.IBaseInfo;
import com.example.taobaounion.model.bean.SelectContent;
import com.example.taobaounion.model.bean.SelectedPageCategory;
import com.example.taobaounion.presenter.ISelectedPagePresenter;
import com.example.taobaounion.ui.adapter.SelectedPageContentAdapter;
import com.example.taobaounion.ui.adapter.SelectedPageLeftAdapter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.PresenterManager;
import com.example.taobaounion.utils.SizeUtils;
import com.example.taobaounion.utils.TicketUtils;
import com.example.taobaounion.view.ISelectPageCallback;

import butterknife.BindView;

public class SelectedFragment extends BaseFragment implements ISelectPageCallback, SelectedPageLeftAdapter.OnLeftItemClickListener, SelectedPageContentAdapter.OnSelectedPageContentItemClickListener {

    private ISelectedPagePresenter mSelectedPagePresenter;

    @BindView(R.id.left_category_list)
    public RecyclerView leftCategoryList;
    @BindView(R.id.right_content_list)
    public RecyclerView rightContentList;
    @BindView(R.id.fragment_bar_title_tv)
    public TextView barTitleTv;
    private SelectedPageLeftAdapter mLeftAdapter;
    private SelectedPageContentAdapter mRightAdapter;

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_selected;
    }
    @Override
    protected void initView(View rootView) {
        setUpState(State.SUCCESS);
        leftCategoryList.setLayoutManager(new LinearLayoutManager(getContext()));//线性布局管理器默认垂直
        mLeftAdapter = new SelectedPageLeftAdapter();
        leftCategoryList.setAdapter(mLeftAdapter);

        rightContentList.setLayoutManager(new LinearLayoutManager(getContext()));
        mRightAdapter = new SelectedPageContentAdapter();
        rightContentList.setAdapter(mRightAdapter);
        rightContentList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull  Rect outRect, @NonNull  View view, @NonNull  RecyclerView parent, @NonNull  RecyclerView.State state) {
               int topAndBottom= SizeUtils.dip2px(getContext(),4);
               int leftAndBottom=SizeUtils.dip2px(getContext(),6);
                outRect.top=topAndBottom;
                outRect.bottom=topAndBottom;
                outRect.left=leftAndBottom;
                outRect.right=leftAndBottom;
            }
        });
        barTitleTv.setText("精选宝贝");
    }
    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_with_bar_layout,container,false);
    }
    @Override
    protected void initPresenter() {
        mSelectedPagePresenter = PresenterManager.getInstance().getSelectedPagePresenter();
        mSelectedPagePresenter.registerViewCallback(this);
        mSelectedPagePresenter.getCategories();
    }

    @Override
    protected void initListener() {
        mLeftAdapter.setOnLeftItemClickListener(this);
        mRightAdapter.setOnSelectedPageContentItemClickListener(this);
    }

    @Override
    protected void release() {
        if (mSelectedPagePresenter != null) {
            mSelectedPagePresenter.unregisterViewCallback(this);
        }
    }

    @Override
    public void onCategoriesLoaded(SelectedPageCategory categories) {
        setUpState(State.SUCCESS);
        mLeftAdapter.setData(categories);
        //获取分类内容
        LogUtils.d(this,"onCategoriesLoaded--->"+categories);
        //根据当前选中的分类，获取分类详情内容
//        List<SelectedPageCategory.DataBean> data = categories.getData();
//        mSelectedPagePresenter.getContentByCategory(data.get(0));
    }

    @Override
    public void onContentLoad(SelectContent content) {
//        LogUtils.d(this,"onContentLoad->"+content.getData()
//                .getTbk_dg_optimus_material_response()
//                .getResult_list()
//                .getMap_data()
//                .get(0)
//                .getTitle()
//        );
        mRightAdapter.setData(content);
        //每次数据回来后，去设置适配器，刷新数据后，显示出来，并设置他滚动的位置是第0个位置
        rightContentList.scrollToPosition(0);

    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {

        setUpState(State.LOADING);

    }

    @Override
    public void onEmpty() {

    }

    @Override
    protected void onRetryClick() {
        if (mSelectedPagePresenter != null) {
            mSelectedPagePresenter.reloadContent();
        }
    }

    @Override
    public void onLeftItemClick(SelectedPageCategory.DataBean item) {
        LogUtils.d(this,"item--->"+item.getFavorites_title());
        //左边的分类点击了
        mSelectedPagePresenter.getContentByCategory(item);
    }

    @Override
    public void onContentItemClick(IBaseInfo item) {
        TicketUtils.toTicketPage(getContext(),item);
    }
}
